<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>求值顺序</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/eval_order","wgTitle":"cpp/language/eval order","wgCurRevisionId":64607,"wgArticleId":2709,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with unreviewed CWG DR marker"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/eval_order","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_eval_order skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">求值顺序</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="expressions.html" title="cpp/language/expressions"> 表达式</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> 概述 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="value_category.html" title="cpp/language/value category"> 值类别</a>（左值 lvalue、右值 rvalue、亡值 xvalue）</td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 求值顺序</strong>（序列点）</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="constant_expression.html" title="cpp/language/constant expression"> 常量表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="expressions.html#.E4.B8.8D.E6.B1.82.E5.80.BC.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/expressions"> 不求值表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="expressions.html#.E5.88.9D.E7.AD.89.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/expressions"> 初等表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lambda.html" title="cpp/language/lambda">lambda 表达式</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 字面量 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="integer_literal.html" title="cpp/language/integer literal">整数字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="floating_literal.html" title="cpp/language/floating literal">浮点字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bool_literal.html" title="cpp/language/bool literal">布尔字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="character_literal.html" title="cpp/language/character literal">字符字面量</a>，包含<a href="escape.html" title="cpp/language/escape">转义序列</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="string_literal.html" title="cpp/language/string literal">字符串字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="nullptr.html" title="cpp/language/nullptr">空指针字面量</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="user_literal.html" title="cpp/language/user literal">用户定义字面量</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 运算符 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_assignment.html" title="cpp/language/operator assignment"> 赋值运算符</a>:
<code>a=b</code>, <code>a+=b</code>, <code>a-=b</code>, <code>a*=b</code>, <code>a/=b</code>, <code>a%=b</code>, <code>a&amp;=b</code>, <code>a|=b</code>, <code>a^=b</code>, <code>a&lt;&lt;=b</code>, <code>a&gt;&gt;=b</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_incdec.html" title="cpp/language/operator incdec"> 自增与自减</a>: <code>++a</code>, <code>--a</code>, <code>a++</code>, <code>a--</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_arithmetic.html" title="cpp/language/operator arithmetic"> 算术运算符</a>:
<code>+a</code>, <code>-a</code>, <code>a+b</code>, <code>a-b</code>, <code>a*b</code>, <code>a/b</code>, <code>a%b</code>, <code>~a</code>, <code>a&amp;b</code>, <code>a|b</code>, <code>a^b</code>, <code>a&lt;&lt;b</code>, <code>a&gt;&gt;b</code> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_logical.html" title="cpp/language/operator logical"> 逻辑运算符</a>: <code>a||b</code>, <code>a&amp;&amp;b</code>, <code>!a</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_comparison.html" title="cpp/language/operator comparison"> 比较运算符</a>: <code>a==b</code>, <code>a!=b</code>, <code>a&lt;b</code>, <code>a&gt;b</code>, <code>a&lt;=b</code>, <code>a&gt;=b</code>, <code>a&lt;=&gt;b</code><span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_member_access.html" title="cpp/language/operator member access"> 成员访问运算符</a>: <code>a[b]</code>, <code>*a</code>, <code>&amp;a</code>, <code>a-&gt;b</code>, <code>a.b</code>, <code>a-&gt;*b</code>, <code>a.*b</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_other.html" title="cpp/language/operator other"> 其他运算符</a>: <code>a(...)</code>, <code>a,b</code>, <code>a?b:c</code> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="default_comparisons.html" title="cpp/language/default comparisons">默认比较</a><span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_alternative.html" title="cpp/language/operator alternative">运算符的代用表示</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_precedence.html" title="cpp/language/operator precedence">优先级和结合性</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="fold.html" title="cpp/language/fold">折叠表达式</a><span class="t-mark-rev t-since-cxx17">(C++17)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="new.html" title="cpp/language/new">new 表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="delete.html" title="cpp/language/delete">delete 表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="throw.html" title="cpp/language/throw">throw 表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="alignof.html" title="cpp/language/alignof">alignof</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sizeof.html" title="cpp/language/sizeof">sizeof</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sizeof....html" title="cpp/language/sizeof...">sizeof...</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="typeid.html" title="cpp/language/typeid">typeid</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="noexcept.html" title="cpp/language/noexcept">noexcept</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operators.html" title="cpp/language/operators">运算符重载</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 类型转换 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="implicit_cast.html" title="cpp/language/implicit conversion">隐式转换</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="const_cast.html" title="cpp/language/const cast">const_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_cast.html" title="cpp/language/static cast">static_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reinterpret_cast.html" title="cpp/language/reinterpret cast">reinterpret_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="dynamic_cast.html" title="cpp/language/dynamic cast">dynamic_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit_cast.html" title="cpp/language/explicit cast">显式转换</a> <code>(T)a</code>, <code>T(a)</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="cast_operator.html" title="cpp/language/cast operator">用户定义转换</a></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>求值任何表达式的任何部分，包括求值函数参数的顺序都是<i>未说明</i>的（除了下列的一些例外）。编译器能以任何顺序求值任何操作数和其他子表达式，并且可以在再次求值同一表达式时选择另一顺序。
</p><p>C++ 中无从左到右或从右到左求值的概念。这不会与运算符的从左到右及从右到左结合性混淆：表达式 <code>a() + b() + c()</code> 由于 operator+ 的从左到右结合性被分析成 <code>(a() + b()) + c()</code> ，但可在运行时首先或者最后或者 <code>a()</code> 和 <code>b()</code> 之间对 <code>c</code> 函数调用求值：
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;cstdio&gt;</span>
<span class="kw4">int</span> a<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <a href="../io/c/puts.html"><span class="kw1830">std::<span class="me2">puts</span></span></a><span class="br0">(</span><span class="st0">"a"</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="kw4">int</span> b<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <a href="../io/c/puts.html"><span class="kw1830">std::<span class="me2">puts</span></span></a><span class="br0">(</span><span class="st0">"b"</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="kw4">int</span> c<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <a href="../io/c/puts.html"><span class="kw1830">std::<span class="me2">puts</span></span></a><span class="br0">(</span><span class="st0">"c"</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="kw4">void</span> z<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">int</span>, <span class="kw4">int</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    z<span class="br0">(</span>a<span class="br0">(</span><span class="br0">)</span>, b<span class="br0">(</span><span class="br0">)</span>, c<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>       <span class="co1">// 允许全部 6 种输出排列</span>
    <span class="kw1">return</span> a<span class="br0">(</span><span class="br0">)</span> <span class="sy2">+</span> b<span class="br0">(</span><span class="br0">)</span> <span class="sy2">+</span> c<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 允许全部 6 种输出排列</span>
<span class="br0">}</span></pre></div></div>
<p>可能的输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">b
c
a
c
a 
b</pre></div></div> 
</div>
<p><br>
</p>
<h2><span class="mw-headline" id=".E2.80.9C.E6.8C.89.E9.A1.BA.E5.BA.8F.E6.97.A9.E4.BA.8E.E2.80.9D.E8.A7.84.E5.88.99_.28C.2B.2B11_.E8.B5.B7.29">“按顺序早于”规则 <span class="t-mark-rev t-since-cxx11">(C++11 起)</span></span></h2>
<h3><span class="mw-headline" id=".E5.AE.9A.E4.B9.89">定义</span></h3>
<h4><span class="mw-headline" id=".E8.A1.A8.E8.BE.BE.E5.BC.8F.E6.B1.82.E5.80.BC">表达式求值</span></h4>
<p>每个表达式的求值包括：
</p>
<ul><li> <i>值计算</i>：计算表达式所返回的值。这可能涉及确定对象的身份（泛左值的求值，例如当表达式返回到某对象的引用时），或读取先前赋给对象的值（纯右值的求值，例如当表达式返回数或某个其他值时）
</li><li> 引发<i>副作用</i>：访问（读或写）<code>volatile</code> 泛左值所指代的对象，修改（写入）对象，调用库 I/O 函数，或调用任何做出这些操作的函数。
</li></ul>
<h4><span class="mw-headline" id=".E9.A1.BA.E5.BA.8F">顺序</span></h4>
<p>“按顺序早于 (sequenced-before)”是同一线程中的求值之间的非对称的、传递的对偶关系。
</p>
<ul><li> 若 A 按顺序早于 B，则 A 的求值将在 B 的求值开始前完成。
</li><li> 若 A 不按顺序早于 B 而 B 按顺序早于 A，则 B 的求值将在 A 的求值开始前完成。
</li><li> 若 A 不按顺序早于 B 而 B 不按顺序早于 A，则存在两种可能：
<ul><li> A 与 B 的求值是无顺序 (unsequenced) 的：它们能以任何顺序进行，并可能重叠（在同一执行线程内，编译器可以将组成 A 与 B 的 CPU 指令交错）
</li><li> A 与 B 的求值是顺序不确定 (indeterminately sequenced) 的：它们可以任意顺序进行但不可重叠，A 在 B 前完成，或 B 在 A 前完成。下次求值相同表达式时顺序可以相反。
</li></ul>
</li></ul>
<h3><span class="mw-headline" id=".E8.A7.84.E5.88.99">规则</span></h3>
<div class="t-li1"><span class="t-li">1)</span> 下列<i>全表达式</i>：
<ul><li> <a href="expressions.html#.E4.B8.8D.E6.B1.82.E5.80.BC.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/expressions">不求值操作数</a>
</li><li> <a href="constant_expression.html" title="cpp/language/constant expression">常量表达式</a>
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<ul><li> <a href="consteval.html" title="cpp/language/consteval">立即调用</a>
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<ul><li> 整个<a href="initialization.html" title="cpp/language/initialization">初始化器</a>，包含任何逗号分隔的成分表达式
</li><li> 在非临时对象生存期末尾生成的析构函数调用
</li><li> 不是其他全表达式一部分的表达式（例如整个<a href="statements.html#.E8.A1.A8.E8.BE.BE.E5.BC.8F.E8.AF.AD.E5.8F.A5" title="cpp/language/statements">表达式语句</a>，<a href="for.html" title="cpp/language/for"><code>for</code></a>/<a href="while.html" title="cpp/language/while"><code>while</code></a> 循环的控制表达式，<a href="if.html" title="cpp/language/if"><code>if</code></a>/<a href="switch.html" title="cpp/language/switch"><code>switch</code></a> 的条件表达式，<a href="return.html" title="cpp/language/return"><code>return</code></a> 语句中的表达式，等等），</li></ul></div>

<div class="t-li1"><span class="t-li"></span> 其值计算和副作用，包括应用到表达式结果的隐式转换，对临时量的析构函数调用，（初始化聚合体时）默认成员初始化器，和涉及函数调用的所有其他的语言构造，都<i>按顺序早于</i>下一个<i>全表达式</i>的每个值计算和副作用。</div>
<div class="t-li1"><span class="t-li">2)</span> 任何<a href="expressions.html#.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/expressions">运算符</a>的各操作数的值计算（但非副作用）均<i>按顺序早于</i>该运算符结果的值计算（但非副作用）。</div>
<div class="t-li1"><span class="t-li">3)</span> 调用函数时（无论函数是否内联，且无论是否使用显式函数调用语法），与任何实参表达式或与指代被调用函数的后缀表达式关联的每个值计算和副作用，都<i>按顺序早于</i>被调用函数体内的每个表达式或语句的执行。</div>
<div class="t-li1"><span class="t-li">4)</span> 内建<a href="operator_incdec.html" title="cpp/language/operator incdec">后自增与后自减</a>运算符的值计算<i>按顺序早于</i>其副作用。</div>
<div class="t-li1"><span class="t-li">5)</span> 内建<a href="operator_incdec.html" title="cpp/language/operator incdec">前自增与前自减</a>运算符的副作用<i>按顺序早于</i>其值计算（作为由复合赋值的定义所致的隐含规则）。</div>
<div class="t-li1"><span class="t-li">6)</span> 内建<a href="operator_logical.html" title="cpp/language/operator logical">逻辑</a>与 (AND) 运算符 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy3">&amp;&amp;</span></span></span> 和内建逻辑或 (OR) 运算符 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy3">||</span></span></span> 的第一（左）操作数的每个值计算和副作用，<i>按顺序早于</i>第二（右）操作数的每个值计算和副作用。</div>
<div class="t-li1"><span class="t-li">7)</span> 与<a href="operator_other.html#.E6.9D.A1.E4.BB.B6.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/operator other">条件运算符</a> <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy4">?:</span></span></span> 中的第一个表达式关联的每个值计算和副作用，都<i>按顺序早于</i>与第二或第三表达式关联的每个值计算和副作用。</div>
<div class="t-li1"><span class="t-li">8)</span> 内建<a href="operator_assignment.html#.E5.86.85.E5.BB.BA.E7.9A.84.E7.9B.B4.E6.8E.A5.E8.B5.8B.E5.80.BC" title="cpp/language/operator assignment">赋值</a>运算符和所有内建<a href="operator_assignment.html#.E5.86.85.E5.BB.BA.E7.9A.84.E5.A4.8D.E5.90.88.E8.B5.8B.E5.80.BC" title="cpp/language/operator assignment">复合</a>赋值运算符的副作用（修改左参数），均<i>按顺序晚于</i>左右参数的值计算（但非副作用），且<i>按顺序早于</i>赋值表达式的值计算（即早于返回指代被修改对象的引用之时）。</div>
<div class="t-li1"><span class="t-li">9)</span> 内建<a href="operator_other.html#.E5.86.85.E5.BB.BA.E7.9A.84.E9.80.97.E5.8F.B7.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/operator other">逗号运算符</a> <span class="t-c"><span class="mw-geshi cpp source-cpp">,</span></span> 的第一个（左）参数的每个值计算和副作用均<i>按顺序早于</i>第二个（右）参数的每个值计算和副作用。</div>
<div class="t-li1"><span class="t-li">10)</span> <a href="list_initialization.html" title="cpp/language/list initialization">列表初始化</a>中，在大括号中用逗号分隔的任何给定的初始化器子句的每个值计算和副作用都<i>按顺序早于</i>逗号后的任何给定的初始化器子句的每个值计算和副作用</div>
<div class="t-li1"><span class="t-li">11)</span> 若某个函数调用既不<i>按顺序早于</i>又不<i>按顺序晚于</i>另一函数调用，则它们是<i>顺序不确定的</i>（程序必须表现为<a href="as_if.html" title="cpp/language/as if">如同</a>组成不同函数调用的 CPU 指令决不会交错，即使函数被内联也是如此）。
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<p>规则 11 有一个例外：在 <a href="../algorithm/execution_policy_tag_t.html" title="cpp/algorithm/execution policy tag t"><tt>std::execution::par_unseq</tt></a> 执行策略下执行的标准库算法所作的函数调用是<i>无顺序的</i>，并且可以任意交错。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table></div>
<div class="t-li1"><span class="t-li">12)</span> 对分配函数（<a href="../memory/new/operator_new.html" title="cpp/memory/new/operator new"><tt>operator new</tt></a>）的调用相对于 <a href="new.html" title="cpp/language/new">new 表达式</a>中构造函数参数的求值来说，<span class="t-rev-inl t-until-cxx17"><span>是顺序不确定的</span> <span><span class="t-mark-rev t-until-cxx17">(C++17 前)</span></span></span><span class="t-rev-inl t-since-cxx17"><span>按顺序早于它</span> <span><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></span></span>。</div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<div class="t-li1"><span class="t-li">13)</span> 从函数返回时，作为求值函数调用结果的临时量的复制初始化<i>按顺序早于</i>在 <a href="return.html" title="cpp/language/return">return 语句</a>的操作数末尾处对所有临时量的销毁，而这些销毁进一步<i>按顺序早于</i>对环绕 return 语句的块的所有局部变量的销毁。</div>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<div class="t-li1"><span class="t-li">14)</span> 函数调用表达式中，指名函数的表达式按顺序早于每个参数表达式和每个默认实参。</div>
<div class="t-li1"><span class="t-li">15)</span> 函数调用表达式中，每个形参的初始化的值计算和副作用相对于任何其他形参的初始化的值计算和副作用是顺序不确定的。</div>
<div class="t-li1"><span class="t-li">16)</span> 用运算符写法进行调用时，每个重载的运算符均遵循其所重载的内建运算符的定序规则。</div>
<div class="t-li1"><span class="t-li">17)</span> 下标表达式 <code>E1[E2]</code> 中，E1 的每个值计算和副作用均按顺序早于 E2 的每个值计算和副作用。</div>
<div class="t-li1"><span class="t-li">18)</span> 成员指针表达式 <code>E1.*E2</code> 或 <code>E1-&gt;*E2</code> 中，E1 的每个值计算和副作用都按顺序早于 E2 的每个值计算和副作用（除非 E1 的动态类型不含 E2 所指的成员）。</div>
<div class="t-li1"><span class="t-li">19)</span> 移位运算符表达式 <code>E1&lt;&lt;E2</code> 和 <code>E1&gt;&gt;E2</code> 中，E1 的每个值计算和副作用都按顺序早于 E2 的每个值计算和副作用。</div>
<div class="t-li1"><span class="t-li">20)</span> 每个简单赋值表达式 <code>E1=E2</code> 和每个复合赋值表达式 <code>E1@=E2</code> 中，E2 的每个值计算和副作用均按顺序早于 E1 的每个值计算和副作用。</div>
<div class="t-li1"><span class="t-li">21)</span> 带括号的初始化器中的逗号分隔的表达式列表中的每个表达式，如同函数调用一般求值（顺序不确定）。</div>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E6.9C.AA.E5.AE.9A.E4.B9.89.E8.A1.8C.E4.B8.BA">未定义行为</span></h3>
<p>1) 若标量对象上的一项副作用相对于同一标量对象上的另一副作用为无顺序，则<a href="ub.html" title="cpp/language/ub">其行为未定义</a>。
</p><p><span class="t-c"><span class="mw-geshi cpp source-cpp">i <span class="sy1">=</span> <span class="sy2">++</span>i <span class="sy2">+</span> <span class="nu0">2</span><span class="sy4">;</span>       <span class="co1">// C++11 前为未定义行为</span><br>
i <span class="sy1">=</span> i<span class="sy2">++</span> <span class="sy2">+</span> <span class="nu0">2</span><span class="sy4">;</span>       <span class="co1">// C++17 前为未定义行为</span><br>
f<span class="br0">(</span>i <span class="sy1">=</span> <span class="sy2">-</span><span class="nu0">2</span>, i <span class="sy1">=</span> <span class="sy2">-</span><span class="nu0">2</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// C++17 前为未定义行为</span><br>
f<span class="br0">(</span><span class="sy2">++</span>i, <span class="sy2">++</span>i<span class="br0">)</span><span class="sy4">;</span>       <span class="co1">// C++17 前为未定义行为，C++17 起为未指明</span><br>
i <span class="sy1">=</span> <span class="sy2">++</span>i <span class="sy2">+</span> i<span class="sy2">++</span><span class="sy4">;</span>     <span class="co1">// 未定义行为</span></span></span>
</p><p>2) 若标量对象上的副作用相对于使用同一标量对象的值的值计算为无顺序，则<a href="ub.html" title="cpp/language/ub">其行为未定义</a>。
</p><p><span class="t-c"><span class="mw-geshi cpp source-cpp">cout <span class="sy1">&lt;&lt;</span> i <span class="sy1">&lt;&lt;</span> i<span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// C++17 前为未定义行为</span><br>
a<span class="br0">[</span>i<span class="br0">]</span> <span class="sy1">=</span> i<span class="sy2">++</span><span class="sy4">;</span>       <span class="co1">// C++17 前为未定义行为</span><br>
n <span class="sy1">=</span> <span class="sy2">++</span>i <span class="sy2">+</span> i<span class="sy4">;</span>      <span class="co1">// 未定义行为</span></span></span>
</p>
<h2><span class="mw-headline" id=".E5.BA.8F.E5.88.97.E7.82.B9.E8.A7.84.E5.88.99_.28C.2B.2B11_.E5.89.8D.29">序列点规则 <span class="t-mark-rev t-until-cxx11">(C++11 前)</span></span></h2>
<h3><span class="mw-headline" id=".E5.AE.9A.E4.B9.89_2">定义</span></h3>
<p>表达式的求值可能产生副作用，包括：访问 <code>volatile</code> 左值所指代的对象，修改对象，调用库 I/O 函数，或调用做任何这些动作的函数。
</p><p><i>序列点 (sequence point)</i>是执行序列中的点，在该点所有来自序列中先前求值的副作用均已完成，而后继求值的副作用都未开始。
</p>
<h3><span class="mw-headline" id=".E8.A7.84.E5.88.99_2">规则</span></h3>
<p>1) 每个全表达式结尾（典型地在分号处）有一个序列点。
</p><p>2) 调用函数时（无论该函数是否内联，无论是否使用函数调用语法），所有函数实参的求值（若存在）之后有一个序列点，它发生于函数体内的任何表达式或语句的执行之前。
</p><p>3) 对函数的返回值进行复制之后，并在函数外任何表达式的执行之前有一个序列点。
</p><p>4) 一旦函数执行开始，则在被调用函数的执行完成前，不求值调用方函数的任何表达式（函数不能交错执行）。
</p><p>5) 每个使用内建（非重载）运算符的下列四种表达式的求值中，表达式 <code>a</code> 的求值后有一个序列点。
</p><p><span class="t-c"><span class="mw-geshi cpp source-cpp">a <span class="sy3">&amp;&amp;</span> b<br>
a <span class="sy3">||</span> b<br>
a <span class="sy4">?</span> b <span class="sy4">:</span> c<br>
a , b</span></span>
</p>
<h3><span class="mw-headline" id=".E6.9C.AA.E5.AE.9A.E4.B9.89.E8.A1.8C.E4.B8.BA_2">未定义行为</span></h3>
<p>1) 前后序列点间，至多可以修改标量对象的存储值一次，否则<a href="ub.html" title="cpp/language/ub">其行为未定义</a>。
</p><p><span class="t-c"><span class="mw-geshi cpp source-cpp">i <span class="sy1">=</span> <span class="sy2">++</span>i <span class="sy2">+</span> i<span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// 未定义行为</span><br>
i <span class="sy1">=</span> i<span class="sy2">++</span> <span class="sy2">+</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// 未定义行为（ C++17 前）</span><br>
i <span class="sy1">=</span> <span class="sy2">++</span>i <span class="sy2">+</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// 未定义行为（ C++11 前）</span><br>
<span class="sy2">++</span> <span class="sy2">++</span>i<span class="sy4">;</span> <span class="co1">// 未定义行为（ C++11 前）</span><br>
f<span class="br0">(</span><span class="sy2">++</span>i, <span class="sy2">++</span>i<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 未定义行为（ C++17 前）</span><br>
f<span class="br0">(</span>i <span class="sy1">=</span> <span class="sy2">-</span><span class="nu0">1</span>, i <span class="sy1">=</span> <span class="sy2">-</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 未定义行为（ C++17 前）</span></span></span>
</p><p>2) 前后序列点间，访问表达式求值所修改的标量对象的先前值，必须只为确定要存储的值。若以任何其他方式访问，则<a href="ub.html" title="cpp/language/ub">其行为未定义</a>。
</p><p><span class="t-c"><span class="mw-geshi cpp source-cpp">cout <span class="sy1">&lt;&lt;</span> i <span class="sy1">&lt;&lt;</span> i<span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// 未定义行为（ C++17 前）</span><br>
a<span class="br0">[</span>i<span class="br0">]</span> <span class="sy1">=</span> i<span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// 未定义行为（ C++17 前）</span></span></span>
</p>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue1885">CWG 1885</a>
</td>
<td> C++14
</td>
<td> 函数返回时自动变量的销毁顺序不明确
</td>
<td> 添加顺序规则
</td></tr></table>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<div class="t-ref-std-c++11">
<ul><li> C++11 standard (ISO/IEC 14882:2011): 
</li></ul>
<dl><dd><ul><li> 1.9 Program execution [intro.execution]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 5.2.6 Increment and decrement [expr.post.incr]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 5.3.4 New [expr.new]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 5.14 Logical AND operator [expr.log.and]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 5.15 Logical OR operator [expr.log.or]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 5.16 Conditional operator [expr.cond]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 5.17 Assignment and compound assignment operators [expr.ass]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 5.18 Comma operator [expr.comma]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 8.5.4 List-initialization [dcl.init.list]
</li></ul>
</dd></dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<ul><li> <a href="operator_precedence.html" title="cpp/language/operator precedence">运算符优先级</a>，定义从表达式的源码表示构建它的方式。
</li></ul>
<table class="t-dsc-begin">

</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 2625/1000000
Preprocessor generated node count: 7588/1000000
Post‐expand include size: 67497/2097152 bytes
Template argument size: 14537/2097152 bytes
Highest expansion depth: 13/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:2709-0!*!0!!zh!*!zh!* and timestamp 20200223155947 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/eval_order&amp;oldid=64607">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/eval_order&amp;oldid=64607</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/eval_order">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年2月9日 (星期日) 21:40。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过15,575次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.975 secs. -->
	</body>
<!-- Cached 20200223155947 -->
</html>